package com.zkgengkun.support.snowflake;//

import java.util.Random;

public class DaoHelper {
    private static final ThreadLocal<Long> countHolder = new ThreadLocal();
    private static final ThreadLocal<Long> primaryKeyHolder = new ThreadLocal();
    private static final ThreadLocal<Long[]> primaryKeysHolder = new ThreadLocal();
    private static final IdWorker idWorker = UniqueIDHolder.getIdWorker();
    private static final Random random = new Random();

    public DaoHelper() {
    }


    public static Long createPrimaryKey(Long number) throws Exception {
        return Long.valueOf(idWorker.nextId(number));
    }

    public static Long createRandomPrimaryKey() throws Exception {
        return Long.valueOf(idWorker.nextId((long) (random.nextInt(1024) + 1)));
    }

    protected static void setCount(Long count) {
        countHolder.set(count);
    }

    public static Long getPrimaryKey() {
        return (Long) primaryKeyHolder.get();
    }

    protected static void setPrimaryKey(Long primaryKey) {
        primaryKeyHolder.set(primaryKey);
    }

    public static Long[] getPrimaryKeys() {
        return (Long[]) primaryKeysHolder.get();
    }

    protected static void setPrimaryKeys(Long[] primaryKeys) {
        primaryKeysHolder.set(primaryKeys);
    }

}
